<?php
// anti direct-access
if (!defined('MEKARSARI_WEBGIS'))
	exit('Direct access to this script is prohibited.');

class Edit{
	var $db;
	var $config;
	var $data;
	var $form;
	
	function Edit($dbObject, $config=''){
		$this->db = $dbObject;
		$this->config  = $config;
	}
	
	function makeTitle($str){
		$out = preg_replace('/_/', ' ', $str);
		
		return ucwords($out);
	}
	
	function createForm($bagian, $subbagian, $q){
		$this->subbagian = $subbagian;
		$this->data = $this->db->get_row($q, 0, ARRAY_A);	
		
		$tableConfig = $this->config[$subbagian];
		
		// make up the form
		$this->form['name'] = 'edit';
		
		// prepare field
		foreach($this->data as $key=>$val){			
			if( in_array($key, $tableConfig['primary']) ){			
				$this->form['field'][] = array(
					'name'	=> $key,
					'value'	=> $val,
					'type'	=> 'readonly="readonly"');
			}
			else{				
				$this->form['field'][] = array(
					'name'	=> $key,
					'value'	=> $val,
					'type'	=> '');			
			}
		}
		
		$fields = array_keys($this->data);
		
		//dependencies, 
		$alat = array(
			'peralatan',
			'pupuk',
			'insektisida',
			'herbisida',
			'fungisida',
			'bahan_kimia_lain',
		);
		
		$jml_alat = array(
			'jml_alat',
			'kadar_pupuk',
			'kadar_insektisida',
			'kadar_herbisida',
			'kadar_fungisida',
			'kadar_bahan_kimia_lain',
		);
		
		$hash  = array_combine($alat, $jml_alat);
		$hash2 = array_combine($jml_alat, $alat);
		
		$script = "
		<script>		
			function inArray(a, arr){
				for(var i=0; i<arr.length; i++){
					if(arr[i] == a)
						return i;						
				}
				
				return -1;
			}
			
			function checkField(form){
				var invalidNumber = /[^0-9.,]/;
				var a = new Array(6);
				var b = new Array(6);				
		";
		for($i=0; $i<count($alat); $i++){
			$script .= "a[$i] = '".$alat[$i]."';";
			$script .= "b[$i] = '".$jml_alat[$i]."';";
		}
		
		include 'config/number_config.php';
		foreach($fields as $field){
			// dependencies
			// nama alat diisi, cek kadarnya
			if( in_array($field, $alat) ){
				$a = $field;
				$b = $hash[$field];
				
				$script .= "
				if( form.$a.value.length > 0 ){
					if( form.$b.value.length < 1 ){
						alert('Field : \"$b\" harus diisi');
						form.$b.focus(); 

						return false;
					}
				}
				";
			}
			
			// kadar diisi, nama harus diisi
			if( in_array($field, $jml_alat) ){
				$a = $field;
				$b = $hash2[$field];
				
				$script .= "
				if( form.$a.value.length > 0 ){
					if( form.$b.value.length < 1 ){
						alert('Field : \"$b\" harus diisi');
						form.$b.focus(); 

						return false;
					}
				}
				";
			}

		
			// angka
			if( in_array($field, $numberConfig) ){
			$script .= "			
				if(invalidNumber.test(form.$field.value)){
					alert('Field : \"$field\" harus angka');
					form.$field.value = '0';
					form.$field.style.border = '1px solid red';
					form.$field.focus(); 
					
					return false;
				}
			";
			}
		}
		
		// depedencies.. di perawatan, dll yang ada kadar dllnya..
		$script .= "			
		}
		</script>";
		// print out
		$out = $script.'		
			<form 
				name="'.$this->form['name'].'" id="'.$this->form['name'].'" 
				method="GET" 
				action="edit.php?bagian='.$bagian.'&subbagian='.$subbagian.'"
				onSubmit="return checkField(this);"
				>
				<table align="center" width="450" cellpadding="3" cellspacing="0">
				';
			foreach($this->form['field'] as $f){				
				$out .=				
					  '
					<tr>		
					  <td>'.$this->makeTitle($f['name']).'</td>
					  <td>
						<input 
							type="text" 
							name="'.$f['name'].'" 
							id="'.$f['name'].'" 
							value="'.$f['value'].'" 
							'.$f['type'].'
							/>
					  </td>
					</tr>
				';
			}	
		$out .= '
					<tr>
						<td>&nbsp;</td>
						<td>
						<input type="submit" name="submit" value="Update"/>&nbsp;
						<input type="reset" name="reset" value="Reset"/>&nbsp;
						<input type="button" name="close" value="Close" onclick="window.close();"/></td>
					</tr>
			  </table>
			  <input type="hidden" name="edit" id="edit" value="yes" />
			  <input type="hidden" name="bagian" id="bagian" value="'.$bagian.'" />
			  <input type="hidden" name="subbagian" id="subbagian" value="'.$subbagian.'" />
			  
			</form>
		';
		
		echo $out;
	}
}

	
?>